<?php
/* $Id$ */


require_once "lib/storage/FileUpload.class.php";

require_once "application/logic/UserFrontend.class.php";
require_once "application/logic/Project.class.php";

define ("AUTHORS_MEMBERSHIP_INSERT_OPTION_STRING", "Füge");
define ("AUTHORS_MEMBERSHIP_REMOVE_OPTION_STRING", "Lösche");

class Admin extends UserFrontend {


	function Admin() {
		$this->UserFrontend();
	}

	function admin_show() {

		/* Header senden */
		Header("Content-Type: text/xml; charset=utf-8");
		echo $this->getTemplateProcessor_class()->processPage("application/webpages/tpl/admin.main.tpl.xhtml.php");

		
	}	

	function admin_reset_tables() {

		$this->sql_reset_tables();
	}

	function admin_show_list( $table, $list_html_trigger, $tools, $id ) {


		switch ($tools) {

			case	"details"	:	{
							/* Details */
	
							/** Allgemeine Keywords setzen **/

							/* Admin Toolbar */
							/* Keywords setzen */
							/* - {projects_as_optionlist} */

							$this->getTemplateProcessor_class()->registerKeyword("{projects_as_optionlist}", 		$this->getProjectAsOptionList_html());

							/* - {roles_as_optionlist} */

							$this->getTemplateProcessor_class()->registerKeyword("{roles_as_optionlist}",
								$this->getAuthorsRolesAsOptionList_html() );
				
							/* --- */

							/* Admin Spezifische Toolsseite */

							switch ( $table ) {
								case "authors"	:	{
											/* Admin Spezifische Toolsseite */
											$this->getTemplateProcessor_class()->registerKeyword("{author_details}", $this->get_keyword_author_details( $id ));

											break;
										}

								case "projects" :	{
									/* Project spezifisches */
									$this->getTemplateProcessor_class()->registerKeyword("{project_details}", $this->get_keyword_project_details( $id ));
			
									break;
								}

							}

							/** Details Admin Template als {content} setzen **/

							$this->getTemplateProcessor_class()->registerKeyword("{content}", 
								$this->getTemplateProcessor_class()->processPage("application/webpages/tpl/admin.".$table.".details.tpl.xhtml.php"));

							break;

						}

			case	"delete"	:	{

							switch ($table) {

								case "authors"	: {
	
											$this->sql_del_authors_account_by_id( $id );

											break;
										}

								case "projects"	: {
											$this->sql_del_table_row_by_id( 
												"projects", "project_id", $id
											);

											break;
										}

							}

							//break;

						}

			default			: {

							
							$this->getTemplateProcessor_class()->registerKeyword("{content}", 
								$this->getTableAsList_html( $table, $list_html_trigger )
							);

						}


		};


	}


	/** Autoren Administration **/

	function admin_edit_authors_membership( $id, $option, $project, $role ) {
		
		if ( ($id >= 1) && 
			( ($option == AUTHORS_MEMBERSHIP_INSERT_OPTION_STRING) || 
			  ($option == AUTHORS_MEMBERSHIP_REMOVE_OPTION_STRING) 
			)
		   ) 
		{ 
			/* Splitte ausgewaehlte Eintraege */
			list ($project_id, $project) = sscanf( $project, "%d %s" );
			list ($role_id, $role) 	= sscanf( $role, "%d %s" );			


			if ( $option == AUTHORS_MEMBERSHIP_INSERT_OPTION_STRING ) {
				/* Eintrag vornehmen */

				/* Check auf moeglichen doppelten Eintrag */

				if ( $this->sql_check_memberships_this_row_exists($project_id, $id, $role_id)) {
					/* Eintrag bereits vorhanden */

					$this->setErrorMessage("Mitgliedschaft bereits vorhanden", true);
			
				} else {

					/* Edit Eintrag */
					$this->sql_add_row_table_memberships( $project_id, $id, $role_id );

					$this->setStatusMessage("Membership von Author mit der ID ".$id." wurde neu gesetzt ($project als $role)", false);
				}
			} else {
				/* Remove */

				$row = $this->sql_check_memberships_this_row_exists($project_id, $id, $role_id);
				if ( $row ) {
					/* Existent */

					$this->sql_del_row_table_memberships( $row->membership_id );

				} else {
					/* Existiert gar nicht */

					$this->setErrorMessage("Eine nicht vorhandene Mitgliedschaft kann nicht gel&ouml;scht werden.", true);

				}

			}			


			/* Ausgeben */

			$this->admin_show_authors_list ("details", $id );

		} else
			$this->setErrorMessage( "Falsche Authoren ID oder andere ung&uuml;ltige Parameter!", true );

	}

	function admin_edit_authors_accounthead( $id, $option, $author_name, $author_email ) {

		$nextPage = "details";

		switch ($option) {

			case "save"	: {

						$fileUpload_class = new FileUpload( "fupload_author_avatar" );

						if ( $fileUpload_class->isValidUpload( 
							$this->getProfile_class()->getScriptConfig()->getAllowedAvatarMimeTypes()
						     ) && ($fileUpload_class->getCurrentPostedFileName() != "") 
						   ) {

							$this->saveUploadedAvatarInDatabase( 
								$id, 
								$fileUpload_class,
								array( "plain" ) 
							);

						} else {

							if ( $fileUpload_class->getCurrentPostedFileName() != "" ) {
								/* Fehler beim Upload des Avatars (sofern kein leerstring uebergeben */
								$this->setErrorMessage("Fehler beim upload und verifizieren des Avatars!", true );
							} else {
								$this->setErrorMessage("Eintrag erfolgt ohne Avatar&auml;nderung", true );
							}

						}

						//echo $_REQUEST

						$q= $this->sql_change_authors_account_details_by_id($id, $author_name, $author_email );

						if ($q === false) $this->setErrorMessage( "Datenbankfehler! ".mysql_error(), true ); 
							else $this->setStatusMessage( "Daten wurden eingetragen!", false );
							

						break;
				   	}
			case "remove"	: {
						
						$this->sql_del_authors_account_by_id( $id );
			
						$nextPage = "";
						break;
				     	}
			default		: {
						$this->setErrorMessage("Unbekannte Option!", true ); 
					}

		}

		$this->admin_show_list( "authors", "author_list_html", $nextPage, $id );

	}

	function admin_add_new_author() {

		$author_id = $this->sql_add_row_defaults_table_authors();

		if ($author_id !== false) {

			//$this->admin_show_author_list( "details", $author_id );


		}

	}

	/** Projekt Administration **/

	function admin_add_new_project() {

		$project_id = $this->sql_add_row_defaults_table_projects();

		if ($project_id !== false) {

			//$this->admin_show_author_list( "details", $author_id );


		}

	}

	function admin_edit_project( $id, $option, $project_name, $project_description_short ) {

		switch ($option) {

			case "save"	: {

						$fileUpload_class = new FileUpload( "fupload_author_avatar" );

						if ( $fileUpload_class->isValidUpload( 
							$this->getProfile_class()->getScriptConfig()->getAllowedAvatarMimeTypes()
						     ) && ($fileUpload_class->getCurrentPostedFileName() != "") 
						   ) {

							$this->saveUploadedAvatarInDatabase( 
								$id, 
								$fileUpload_class,
								array( "plain" ) 
							);

						} else {

							if ( $fileUpload_class->getCurrentPostedFileName() != "" ) {
								/* Fehler beim Upload des Avatars (sofern kein leerstring uebergeben */
								$this->setErrorMessage("Fehler beim upload und verifizieren des Avatars!", true );
							} else {
								$this->setErrorMessage("Eintrag erfolgt ohne Avatar&auml;nderung", true );
							}

						}

						//echo $_REQUEST
						
						$this->sql_change_project_details_by_id( $id, $project_name, $project_description_short );

						$this->setStatusMessage("<script type=\"javascript\">document.writeln(\"TEST\"); </script>", true);
				
						/*
						$q= $this->sql_change_authors_account_details_by_id($id, $author_name, $author_email );

						if ($q === false) $this->setErrorMessage( "Datenbankfehler! ".mysql_error(), true ); 
							else $this->setStatusMessage( "Daten wurden eingetragen!", false );
						*/	

						break;
				   	}
			case "remove"	: {
						

						break;
				     	}
			default		: {
						$this->setErrorMessage("Unbekannte Option!", true ); 
					}

		}

		$this->admin_show_projects_list( "details", $id );

	}


	function admin_edit_table_row_by_id( $id, $option, $post_object ) {

		switch ($option) {

			case "save"	:	{
							/* Tabelle anhand der des <tabelle>_id Tags ermitteln */
							//$this->admin_edit_table_get_tablename_by_request ( );
					
							foreach( $_REQUEST as $key=>$value ) {
							//echo "Key: ".$key." Value= ".$value."<br />";
								if ( preg_match( "/_/", $key ) ) {
									/* Datenbankspezifisches Tag */
									$tablename = strtok ( $this->admin_edit_table_find_key_in_request_by_string( "_" ), "_" );
										echo $key." ".$tablename." <br />";

									switch ($tablename."s") {

										case "authors"	: {
												

													break;
												}
										case "projects"	: {
													$this->sql_change_project_details_by_id( $id, $_REQUEST["project_name"], 
														"Halligalli" );
													break;
												}

									}										


								}
							}		

							break;
						}	

			case "remove"	:	{

							break;
						}
			default		:	{

						}
		}

	}

	function admin_edit_table_find_key_in_request_by_string ( $str ) {
		foreach( $_REQUEST as $key=>$value ) {
			//echo "Key: ".$key." Value= ".$value."<br />";
			if ( preg_match( "/".$str."/", $key ) ) {
				return $key;
			}
		}
	}


	function admin_edit_table_get_tablename_by_request ( ) {
		echo "ID: ".$this->admin_edit_table_find_key_in_request_by_string( "_" );

		/* Tabellenname = an erster Position <table>_id */
		$tablename = strtok ( $this->admin_edit_table_find_key_in_request_by_string( "_" ), "_" );

		echo "Tablename: ".$tablename;

	} 


}



?>